package com.scott.crm.holiday.controller;

import com.scott.base.BaseBean;
import com.scott.base.message.DataRes;
import com.scott.base.message.DataUtil;
import com.scott.base.utils.AESUtil;
import com.scott.crm.holiday.bean.HolidayBean;
import com.scott.crm.holiday.dto.HolidayDto;
import com.scott.crm.holiday.service.HolidayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王伟雄
 * @create 2021/11/30 15:43
 */
@CrossOrigin
@RestController
@Slf4j
public class HolidayController {
    @Autowired
    HolidayService service;

    @RequestMapping(value = "/holiday/add",method = RequestMethod.POST)
    public DataRes add(@RequestBody HolidayBean holidayBean) throws Exception{

        if(holidayBean == null){
            return null;
        }
        // 把token转化为userId
        log.info(holidayBean.toString());
        String token = holidayBean.getToken();
        String userId = AESUtil.decrypt(token, AESUtil.KEY);
        holidayBean.setStudentId(Integer.parseInt(userId));

        int counter = service.addByStudent(holidayBean);
        if(counter ==1 ){
            return DataUtil.success(holidayBean);
        }else{
            return DataUtil.errors();
        }
    }

    @RequestMapping(value = "/holiday/update",method = RequestMethod.POST)
    public DataRes updateByTeacher(@RequestBody HolidayDto dto) throws Exception{

        if(dto == null){
            return null;
        }
        int counter = service.updateByTeacher(dto);
        if(counter >=1 ){
            return DataUtil.success(dto);
        }else{
            return DataUtil.errors();
        }
    }

    @RequestMapping(value = "/holiday/findByStudent",method = RequestMethod.POST)
    public DataRes findByStudent(@RequestBody BaseBean bean) throws Exception{
        Integer pageNumber = bean.getPageNumber();
        Integer pageSize = bean.getPageSize();
        log.info(String.valueOf(pageNumber));
        log.info(String.valueOf(pageSize));
        if(pageNumber<1){
            pageNumber=1;
        }
        Integer begin=(pageNumber-1)*pageSize;
        Integer end =pageSize;

        String token = bean.getToken();
        String userId = AESUtil.decrypt(token, AESUtil.KEY);
        Integer id = Integer.parseInt(userId);

        List<HolidayBean> holidayBeans = service.findByStudent(begin,end,id);

        Map<String,Object> rs= new HashMap<>();
        rs.put("holidayBeans",holidayBeans);

        //pageNumber 第几页
        rs.put("pageNumber",pageNumber);
        //pageSize   1页多少条
        rs.put("pageSize",pageSize);

        List<HolidayBean> holidays = service.findByStudent(id);
        if(!CollectionUtils.isEmpty(holidays)){
            Integer totalCounter = holidays.size();
            //totalCounter 一共多少条记录
            rs.put("totalCounter",totalCounter);

            //totalPage 共多少页
            Integer totalPage = totalCounter%pageSize==0?totalCounter/pageSize:totalCounter/pageSize+1;
            rs.put("totalPage",totalPage);

        }
        log.info("roleList="+holidays.toString());
        return DataUtil.success(rs);
    }

    @RequestMapping(value = "/holiday/findByTeacher",method = RequestMethod.POST)
    public DataRes findByTeacher(@RequestBody HolidayBean bean) throws Exception{
        Integer pageNumber = bean.getPageNumber();
        Integer pageSize = bean.getPageSize();
        log.info(String.valueOf(pageNumber));
        log.info(String.valueOf(pageSize));
        if(pageNumber<1){
            pageNumber=1;
        }
        Integer begin=(pageNumber-1)*pageSize;
        Integer end =pageSize;

        String token = bean.getToken();
        String userId = AESUtil.decrypt(token, AESUtil.KEY);
        Integer id = Integer.parseInt(userId);

        bean.setStudentId(id);
        List<HolidayBean> holidayBeans = service.findByTeacherLimit(begin,end,bean);
        Map<String,Object> rs= new HashMap<>();
        rs.put("holidayBeans",holidayBeans);

        //pageNumber 第几页
        rs.put("pageNumber",pageNumber);
        //pageSize   1页多少条
        rs.put("pageSize",pageSize);

        List<HolidayBean> holidays = service.findByTeacher(bean);
        if(!CollectionUtils.isEmpty(holidays)){
            Integer totalCounter = holidays.size();
            //totalCounter 一共多少条记录
            rs.put("totalCounter",totalCounter);

            //totalPage 共多少页
            Integer totalPage = totalCounter%pageSize==0?totalCounter/pageSize:totalCounter/pageSize+1;
            rs.put("totalPage",totalPage);

        }
        log.info("roleList="+holidays.toString());
        return DataUtil.success(rs);
    }

}
